GIT Commands and Scenarios
Git with local repos
git add
: Git to track our filegit diff <filename>
: show only the unstaged changes by default. If wish to show the difference between staged and µcommitted, used--stage
.git commit -a -m <your msg>
: skipping staging and go directly to commit, but this does not work on new files because they are untracked.-a
means to add all known files.git log --all --oneline --decorate --graph
shows the loggit show <commit id>
show details of a specific commitgit add -p
p for patch. Interactively choose hunks of patch between the index and the work tree and add them to the index. This gives the user a chance to review the difference before adding modified contents to the index.git rm <filename>
stops a file being tracked and removing from the directory. changes are staged to be committed.git reset HEAD <filename>
unstages added changesgit mv <filename1> <filename2>
to rename file in the repo. This will also change the working directory.git merge --abort
discard mergegit rebase <branch name>
: move the current branch on top of the. This will help keep the history linear
Git remote command
git remote
: list remote repositoriesgit remote update
: git remote update can update all of your branches set to track remote ones, however not merge any changes ingit fetch
: git fetch can update only the branch you are on, however not merge any changes ingit pull
: can update and merge any remote changes of the present branch you are ongit branch -r
: List remote branches
How to undo changes
reset
vs checkout
reset
resets the index without touching the working treecheckout
changes the working tree without touching the index.
Several usage scenarios
git checkout <filename>
- When to use: stage –> Working
- Will change working directory: yes
- How it works: checkout a file from repo so that the last changes are discarded
This will revert changes to modified files before they are staged.
git checkout head <filename>
- When to use: stage –> stage
- Will change working directory: Yes
- How it works: checkout a file from repo so that the last changes are discarded
This will revert changes to modified files before they are staged.
git reset
- When to use: repo –> stage
- Will change working directory: No
- How it works: unstage changes, reset commit
git reset –-hard origin/master
- When to use: remote repo –> local repo –> working
- Will change working directory: Yes
- How it works: after committing to remote repo
How to modify a previous commit
Fix a (fixable) commit
Scenario: The previous commit misses either some file or the msg is not correct
Solution: use --amend
along with staged changes to modify the previous commit
What it actually does: overwrite the previous commit
Heads up: avoid using it on public repos
1 | git add a.py |
revert a (bad) commit
Scenario: roll back a bad commit
Solution: revert the commit
What it actually does: creates an inverse of the previous commit
1 | git revert HEAD # revert the previous head |
How to squash commits when doing pull request
Use this when forking and doing pull requests back to the master.
rebase to master. We can use rebase since typically only one person uses the forked repo
1
git rebase -i master # I for interactive
squash commits
- make the first commit
1
2
3
4
5git checkout -b new_branch
edit
git add .
git commit -m 'first commit'
git push - make the second commit
1
2
3
4edit
git add .
git commit -m 'second commit'
git push # now new_branch is ahead of master by two commit - rebase and squash. If you mark one or more lines as “squash”, they will be combined with the one above
1
2git rebase -i master #interactively squash commits
git show # show the latest commit
- make the first commit
Miscellaneous
cherry-pick
- use when selecting commits from other branches into main branch
-
git cherry-pick <commitID1> <commitID2>
modify old commits
- use
git rebase -i
to reorder the commit I want to change to be on top - use
git commit --amend
to modify the commit - use
git rebase -i
again to change the order back
tag
- use when I want some anchor more permanent than a branch or commit
-
git tag <versionid> <commitID>
- use
git describe <a reference>
to describe where you are relative to the closest “anchor”. The reference could be a branchrelative referencing
- Moving upwards one commit at a time with
^
1
2git checkout main^
git checkout main^^ - Moving upwards a number of times with
~<num>
1
git branch -f main HEAD~3 # reset main 3 commits upwards
Reference: