Skip to content

Git Commands at a Glance ​

1. Trying Out Git ​

  • git init : sets the current directory as a project directory managed by Git (= working directory) and creates a repository (the .git directory) inside it
  • git config user.name 'your-name' : sets the current user's ID to 'your-name' (information needed when committing)
  • git config user.email 'you@example.com' : sets the current user's email address to 'you@example.com' (information needed when committing)
  • git add [file name] : puts a specific modified file onto the staging area
  • git add [directory name] : puts every modified file in that directory onto the staging area
  • git add . : puts every modified file in the working directory onto the staging area
  • git reset [file name] : takes a file you put on the staging area back off
  • git status : prints what Git currently knows about the project (useful for understanding the current state when something goes wrong)
  • git commit -m "commit message" : records what is currently on the staging area as a commit
  • git help [command name] : prints the official manual for a Git command you want to learn how to use

2. Getting Started with GitHub ​

  • git push -u (or --set-upstream) origin master : used the first time you upload the contents of your local repository to a remote repository.
  • git push : after running the command above once, you can upload the local repository's contents to the remote repository by just typing git push.
  • git pull : after running the command two lines above once, you can bring the remote repository's contents into the local repository by just typing git pull.
  • git clone [the project's address on GitHub] : brings a project on GitHub onto your computer

3. Working with Commits in Git ​

  • git log : prints the commit history

  • git log --pretty=oneline : with the --pretty option, you can print the commit history in many different ways. Giving --pretty the value oneline prints one line per commit. To learn more about the --pretty option, see this link.

  • git show [commit ID] : shows what changes were made in a specific commit

  • git commit --amend : modifies the latest commit again and turns it into a new commit

  • git config alias.[alias] [command] : gives a long command an alias so that from then on you can run it by the alias too

  • git diff [commit A's ID] [commit B's ID] : compares the differences between two commits

  • git reset [option] [commit ID] : what it does depends on the option (if you omit the option, --mixed is applied)

    (1) Moves HEAD to point to the specified commit (--soft stops here)

    (2) Also resets the staging area to match the specified commit (--mixed stops here)

    (3) Also resets the working directory to match the specified commit (--hard stops here)

    Instead of a commit ID, you can also use notation relative to HEAD's position (e.g. HEAD^, HEAD~3)

  • git tag [tag name] [commit ID] : attaches a tag to a specific commit

4. Using Branches in Git ​

  • git branch [new branch name] : creates a new branch
  • git checkout -b [new branch name] : creates a new branch and switches to it right away
  • git branch -d [existing branch name] : deletes a branch
  • git checkout [existing branch name] : switches to that branch
  • git merge [existing branch name] : merges another branch into the current branch
  • git merge --abort : when a conflict occurs during a merge, cancels the merge for now and goes back to the previous state

5. Git in Practice I ​

  • git fetch : in the local repository, fetches the latest commits from the upstream branch of the branch HEAD currently points to (it differs from git pull in that it only fetches, whereas git pull fetches and also merges)
  • git blame : prints which commit created each line of a specific file
  • git revert : creates a new commit that undoes (cancels) the work done in a specific commit

6. Git in Practice Ⅱ ​

  • git reflog : prints the history of commits HEAD has pointed to
  • git log --all --graph : prints the commit history of all branches as a graph that clearly shows the relationships between commits
  • git rebase [branch name] : when there are branches A and B and HEAD currently points to branch A, running git rebase B takes the commits on branch A that exist after the common commit where A and B diverged, and attaches them as they are after the latest commit on branch B (it has the same effect as git merge, but the difference is that the commit history becomes a single clean line)
  • git stash : saves the current work to the stack area
  • git stash apply [commit ID] : applies the most recent (or a specific) saved work in the stack area to the working directory
  • git stash drop [commit ID] : deletes the most recent (or a specific) saved work from the stack area
  • git stash pop [commit ID] : applies the most recent (or a specific) saved work in the stack area to the working directory and deletes it from the stack
  • git cherry-pick [commit ID] : applies the contents of a specific commit to the current commit

! Other things you should know

(1) If you run just git commit, a text editor window opens where you can type the commit message. Once you type the commit message there and save, the commit is made.

(2) git push and git pull work per branch. For example, if you run git push on the master branch, only the contents of the master branch are sent to the remote repository's master branch; the contents of the premium branch are not sent. (You can send the contents of all branches by giving git push the --all option, though.)

Summary ​

StageKey commands
Getting startedgit init, git config, git add, git commit, git status
GitHubgit push -u origin master, git pull, git clone
Working with commitsgit log, git show, git commit --amend, git diff, git reset, git tag
Branchesgit branch, git checkout -b, git merge, git merge --abort
In practicegit fetch, git blame, git revert, git reflog, git rebase, git stash, git cherry-pick

Reference: personal Notion study notes (Git study)