Git Tutorial

Selman Koral
5 min readApr 20, 2022

--

How to use Git?

Who is this article for?

  • Those with no experience with Git
  • Those who like to review their knowledge
  • Those who’d like to see cool and simple Git C H E A T S H E E T S

If you are new to Git, I recommend you to follow these questions to learn smooth:

After completing these steps, let’s move forward.

I recommend you to learn the manual method that you use command-line interface firstly. Then, learn how to do it with VS Code, so that you will understand the basics better. In this article, we will see the command-line method.

How do I use Git and GitHub?

1. Configure Git username and email globally. These details will be associated with your commits. Use without --global flag to set Git username and email for the current repository.

$ git config — global user.name “JohnDoe”
$ git config — global user.email “johndoe@gmail.com”

2. Initialize Git repository with an existing project. Inside project folder:

$ git init

3. Check status. Use whenever you are using git. It will show the current status of the repo and git workflow and possible actions.

$ git status

4. Help. Get help with git commands

$ git help <git command>

5. Cloning an existing repository

$ git clone <REPO_URL>

6. Configuring remote URL for a repo. (Useful when setting up a fork)

$ git remote -v //list down all remotes
$ git remote add origin <REPO_URL> //add origin
$ git remote rename <old-name> <new-name> //rename a remote

7. Add the original repo URL as Upstream. (can use any other name as well)

$ git remote add upstream https://github.com/ORIGINAL_REPOSITORY.git
$ git remote show upstream //details

8. Syncing a fork with upstream. (syncing master branch)

$ git fetch upstream
$ git checkout master
$ git merge upstream/master

9. Branching

$ git branch //list branches
$ git branch -a //list all remote branches
$ git branch <new_branch_name> //create new branch
$ git branch -d <branch> //delete branch
$ git branch -D <branch> //force delete branch
$ git branch -m <rename_branch> //rename$ git checkout <branch> //change current branch
$ git checkout -b <new_branch_name> //create and change branch

10. Setup local branch to track remote branch

$ git branch -u origin/<remote_branch> <local_branch>

11. Delete remote branch

$ git push origin --delete remote-branch-name

12. Push to a remote branch. (create a remote branch if not existing)

$ git push -u origin <BRANCH_NAME>

13. Amend a commit. Add more changes to the last commit. Change commit message.

$ git commit --amend 
$ git commit --amend -m “message”

14. Check changes since the last commit.

$ git diff
$ git diff --staged //difference from the staged(git add) files

15. Git log. (Check commit history)

$ git log
$ git log -n <limit> //limit
$ git log --oneline //Condense
$ git log --stat //stats
$ git log --author="<pattern>" //commits from specific author
$ git log <file> //commits including a file$ git log --graph --decorate --oneline

16 . You can use .gitignore to untrack a file

17. Force push. (Forcefully overwrite remote branch with local commit history)

$ git push origin <your_branch_name> --force

18. Undo = Resetting the Stage. (Unstage)

$ git reset <FILE>

19. Remove all changes since the last commit to the file.

$ git checkout -- <FILE>
$ git reset HEAD <FILE>

20. Move to commit before HEAD, commit files added to stage.

$ git reset --soft HEAD^

21. Remove/undo last commit and changes.

$ git reset --hard HEAD^

22. Stashing. Put our work to a stack until we pull new changes from remote.

$ git stash //stash unstaged changes  
$ git stash save "add style to our site" //stash with a message//pull from remote and merge$ git stash show //show stash
$ git stash show -p //show the diff of stash
$ git stash list$ git stash pop
OR
$ git stash pop stash@{0} //stash@{0} - stash number$ git stash apply

23. Create a branch from the stash

$ git stash branch <BRANCH_NAME> stash@{1} //stash@{1} - stash no.

24. Clean stash

$ git stash drop stash@{1}
$ git stash clear //clear all stashes

25. Tagging. Useful for tracking releases/versions.

$ git tag //list tags
$ git checkout <TAG> //checkout the code at tag(version)
$ git tag -a v0.0.3 -m "version 0.0.3" //create a new tag v0.0.3
$ git push --tags

Check out for advanced usage.

26. Rebase (fetch and rebase)

Rebasing is the process of moving or combining a sequence of commits to a new base commit. Also known as rewriting git history.

$ git fetch
$ git rebase //adds local commits on top of origin commits, no merge commit
$ git rebase -i //rebase interactive session

27. Local rebasing.

Kind of an advanced feature of Git. Please follow the documentation for more information.

$ git checkout <some_branch>
$ git rebase master //rebase master branch to "some_branch"
$ git checkout master
$ git merge <some_branch>

28. Remove a Merge Commit

If you want to remove a merge commit so that it is like it never happened, the command is simply as follows

$ git rebase --onto <mycommit 1> <mycommit 2> <BRANCH>

Cheatsheets

I hope it helps you guys!

References

[1] [2][3][4][5][6]

--

--