Useful git commands

$git log --pretty=format:"%h - %an, %ar : %s" = show git log and specify log output format $git log = check if your local branch is up to date

$git fetch = fetch all code from github to local

$git pull = bring your branch fast forward and up to date

$git remote -v update = see which branches got updated

$git branch --no-merged = list all abandoned not-merged branches

$git branch -d localBranchName

$git push --delete origin YourBranchName = delete YourBranchName from origin after was merged

$git push --mirror URL_to_new_repository = mirror a repo to other new_repository

$ git remote set-url --add --push origin https://github.com/gigadat/gigadat-iac.git/stage = set 'push' to other URL then default one (gigadat-iac-stg)

$git push origin stage = push your code to 'stage' branch into other git repo

$git commit --allow-empty -m "Trigger build" && git push = commit an empty commit and push (useful when you want to test the CI/CD pipeline and don't want to change a file before commit)

$git reset --hard = remove all changes you made on local branch so you can pull from remote branch

$git pull --rebase = when you have changes that were not pushed and you want to cancel them all

$git reset --soft HEAD~1 = un-stage last commit so that your untracked files are moved back to working area

$git rm -r --cached *.DS_Store = remove all .DS_Store files from local git cache. You have to run a git commit and push afterwards.

Git aliases created on local git config

$git config --global alias.slist 'log --pretty=format:"%h - %an, %ar : %s"'

$git config --global alias.tree 'log --all --graph --decorate --oneline --simplify-by-decoration' = this line adds a global config into git for alias "tree" so you can use "git tree" to see the hierarchical order of your branches

Create ssh private and public key

$ssh-keygen -P "" -m PEM -f key_name

$ssh-keygen -t rsa -b 2048 -P "" -C "email@example.com" -m PEM -f file_name

Bring your local branch up to date with remote

$git fetch --all = downloads commits, files, and refs from a remote repository into your local repo

$git branch -a

$git checkout main = switch to "main" branch

$git remote update = bring remote refs up to date so you can see if any changes to remote branch

$git show-branch *master = will show you the commits in all of the branches whose names end in 'master' (eg master and origin/master).

$git status -uno = check the status of local branch you are into

$git pull origin= fast forward your local branch and update it to be the same like remote

Git connection setup and test

$git config --list = lists all local git configuration (username, url etc.)

$git remote -v = view remote fetch and push url's that is setup on your local machine

$ git remote set-url origin new_url = run this command after you rename your git repository on github

$ git config --global user.name "yourUserName"

$ git config --global user.email yourEmail@example.com

$git clone https://remote_repository . = make sure you add a dot at the end of your command

$git clone -b yourBranch https://remote_repository . = only clone a branch yourBranch

https://git-scm.com/book/en/v2/Getting-Started-First-Time-Git-Setup

Delete last commit from local and remote

1. Check HEAD position on your local 

$git log

2. Move HEAD to previous one or more commits back 

$git reset --hard HEAD^ = move one commit back 

$git reset --hard HEAD~3 = move 3 commits back 

$git log = check HEAD position

3. Remove last commit from GitHub 

$git push --force

Create a feature branch "myFeature" from another branch "dev"

Create MyFeature branch off dev.

$ git checkout -b myFeature dev

Do your work and then commit.

$ git commit -am "Your message"

Now merge your changes to dev without a fast-forward

$ git checkout dev

$ git merge --no-ff myFeature 

Now push changes to the server 

$ git push origin dev

$ git push origin myFeature

Merge your featureBranch into master

$git checkout master

$git pull origin master 

$git merge featureBranch 

$git push origin master

- 'merge' will move HEAD of your branch to 'master' merging the 2 branches

- 'PR' will keep the history of your branch as is and merge into 'master' (recommended)

Create local branch

You can create a branch locally as long as you have a cloned version of the repo. 1. From your terminal window, list the branches on your repository.

$ git branch

* master

This output indicates there is a single branch, the master and the asterisk indicates it is currently active.

2. After you create a branch, you need to check it out from your local system. Use the fetch and checkout commands that GitHub provides, similar to the following:

$ git fetch && git checkout <NewBranch>

3. Make your changes locally and then add, commit, and push your changes to the <NewBranch> branch:

$ git add .

$ git commit -m "adding a change from the feature branch"

$ git push origin <NewBranch>

4. Click the Branch drop down menu of your repository. You should see both the master and the <NewBranch> branch in the branches dropdown. When you make commits to the feature branch, you'll see the files specific to that branch.

You can create a branch locally as long as you have a cloned version of the repo. 1. From your terminal window, list the branches on your repository.

$ git branch

* master

This output indicates there is a single branch, the master and the asterisk indicates it is currently active.

2. Create a new feature branch in the repository

$ git branch <feature_branch>

3. Switch to the feature_branch to work on it.

$ git checkout <feature_branch>

You can list the branches again with the "git branch -a"command. 

4. Commit the change to the feature_branch:

$ git add .

$ git commit -m "adding a change from the feature branch"

5. Switch back to the master branch.

$ git checkout master

6. Push the feature_branch to github master branch

$git push origin <feature_branch>

a. Push the feature_branch to another existing old_branch

$git push origin <feature_branch>:<old_branch>

b. Push our new branch into remote repo by creating this new branch into master:

$git push --set-upstream origin master

7. Click on the Branch dropdown menu of your repository in GitHub. You should see both the master and the feature_branch. Select the feature branch to view its recent commits.

8. Push local new_branch to remote old_branch:

$git push --all -u

Git mirror

Why Git mirroring

You can start a project privately and decide to open-source it on GitHub later on.

How can you do that without losing commit history?

Let’s try another example where you want to upgrade your repository server or try and move your Git repo between two servers without losing its history, branches, etc.

What will you do?

Git mirroring can help you answer these questions.

What is Git mirroring

Git mirroring is when a mirror copies the refs & the remote-tracking branches. It’s supposed to be a functionally identical copy that is interchangeable with the original.

Syntax

git clone --mirror old_repo_url destination_repo

In simple words, it will do the following:

• All the refs will be available in target as-is.

• You will get all the tags.

• You will get all the local branches.

• All the remote-tracking branches will be available in the target.

How to use Git mirror

Its a four-step process – I will explain the step-by-step procedure of how you can do that.

Step 1

The first step is straightforward. You have to mirror the Git repository to your local machine. You can use this command:

git clone --mirror <source_repo_url>

So, the above Git command will create a directory with your repository name.

Step 2

Now, you can create a new repository on your destination server. You can use any name on the destination server, but it helps to use the same name (for simplicity).

Step 3

It is time to set the new server URL in your Git repo. You can run the command from inside your git repository folder:

git remote set-url --push origin <destination_repo_url>

Step 4

Time to push your repository:

git push --mirror

By running the above Git command, you are pushing the complete Git repository with all its Commit history and Branches.

Conclusion

With the Git Mirror option, you can save your valuable commit history with other important refs and remote-tracking branches.

Merging your branch via the command line

If you do not want to use the merge button or an automatic merge cannot be performed, you can perform a manual merge on the command line. However, the following steps are not applicable if the base branch is protected.

Step 1: From your project repository, bring in the changes and test.

git fetch origin

git checkout -b create-core-tfinit-code origin/create-core-tfinit-code 

git merge develop

Step 2: Merge the changes and update on GitHub.

git checkout develop

git merge --no-ff create-core-tfinit-code

git push origin develop

Clone or fetch remote branches

First, clone a remote Git repository and cd into it:

 $git clone git://example.com/myproject

 $cd myproject

Next, look at the local branches in your repository:

$ git branch

 * master

But there are other branches hiding in your repository! You can see these using the - a flag:

 $ git branch

 * master

 remotes/origin/HEAD

 remotes/origin/master

  remotes/origin/v1.0-stable

 remotes/origin/experimental

If you just want to take a quick peek at an upstream branch, you can check it out directly:

$ git checkout origin/experimental

But if you want to work on that branch, you'll need to create a local tracking branch, which is done automatically by:

$ git checkout experimental

and you will see

 Branch experimental set up to track remote branch experimental from origin.

 Switched to a new branch 'experimental'

That last line throws some people: "New branch" - huh? It means that the branch is taken from the index and created locally for you. The previous line is more informative as it tells you that the branch is being set up to track the remote branch.

Now, if you look at your local branches, this is what you'll see:

$ git branch

 * experimental

  master

You can track more than one remote repository using git remote.

$ git remote add win32 git://example.com/users/joe/myproject-win32-port

 $ git branch -a

 * master

 remotes/origin/HEAD

 remotes/origin/master

 remotes/origin/v1.0-stable

remotes/origin/experimental

remotes/win32/master

remotes/win32/new-widgets

At this point, things are getting pretty crazy, so run gitk to see what's going on:

$ gitk --all &

Git push to mirror repo

 How to fetch from a repo and push to a mirror repo:

https://docs.github.com/en/repositories/creating-and-managing-repositories/duplicating-a- repository

$git remote -v

   Note: All staging code was removed when pushing from local localBranch to remote remoteBranch repository. Pushing to a mirror repo makes it the same as a source repo. To bring back all staging code, you must create a local branch with all the code and then merge this branch into the remote 'origin/remoteBranch' branch.

Stashing your local code changes to avoid conflicts

When you get code conflicts, you can "stash" your local changes, have a clean working tree, and then "pop" your stashed changes to the latest HEAD.

$git stash

$git stash list - get the index of the stashed code you want to pop back

$git stash pop --index 0

https://git-scm.com/docs/git-stash

Rename your local and remote git branch

Rename a local Git branch

A local Git branch exists only on your computer. You make changes and tests here without other developers noticing. Renaming it can therefore be done quickly.

Alternatively, you have the option to rename the Git branch via the master. To do this, use the following steps:

Renaming a remote Git branch

In a remote repository, you cannot simply rename a Git branch, as this would lead to complications. Instead, you need to delete the old name and then add the branch with the new name. Fortunately, this is not too hard either and can be done with a few simple commands. As with the local branch, you have two options.

However, if you want to rename the remote Git Branch with just one command, you also have the following option.