Git CheatSheet

Siddharth Malani
2 min readMar 30, 2020

--

Cheatsheet for GIT

1. Clone remote into local

git clone <git repo>

git clone https://github.com/Versent/saml2aws.git

2. Create a new branch

git checkout -b <branch name>

git checkout -b “feature/test”

3. Get latest for checked out branch

git pull

4. Checkout branch in local from local repo

git checkout <branch name>

git checkout fix/myotherbranch

5. List commits with description and without

git log

git log --oneline

6. Change, stage and commit files

git add <changed file>

git commit -m "commit message"

7. Push local changes in selected branch to remote repo

git push

8. When you push the branch first time

git push --set-upstream origin <branchname>

9. You can force push your local to remote but be very careful. Never work on master for that reason.

git push -f

10. Find out where you branched off from another branch

This will visually indicate the branching etc so you can find where you started this branch. This will be useful during the squashing of commits mentioned below.

git log --graph --oneline --all

In example above you can see it is in reverse order and the branch topic split off at “6aafd7f” from master

11. Rebase (recommended over merge)

You are on a branch which has diverged too much. You want to get all your changes to play on the head of master. In simple terms all it means is all the changes in your branch will just overlay on the latest in the branch you are rebasing on.

git checkout master

git pull

git checkout feature/testbranch

git rebase master

When you get conflicts simply do as below fix the conflicts by editing the relevant files

git add <changed files/folder>

git rebase --continue

To abort

git rebase --abort

12. Rebase a long running branch

One small difference. If your branch has too many commits you are better off consolidating all the commits into 1 first before rebasing.

Find the commit where you branched off from master. Then from root folder of your local repo.

git reset --soft <commit id>

// you would use 6aafd7f for example above. This will unstage all changed files to be committed as one change. git status git add .

git commit -m "squashed commits to one"

git rebase master

//Note: always pull master first before rebasing. You can also rebase on another branch.

13. Delete a local branch

git branch -d branchname

14. Force delete a local branch

git branch -D branchname

15. Copy git repo with history & branches

git clone --bare https://github.com/EXAMPLE-USER/OLD-REPOSITORY.git
cd OLD-REPOSITORY.git
git push --mirror https://github.com/EXAMPLE-USER/NEW-REPOSITORY.git
cd ..
rm -rf OLD-REPOSITORY.git

--

--

No responses yet