Of all my git habits, these are by far the most common. Oddly and despite mostly using IntelliJ IDEA where there are already sophisticated GUI interfaces, I use the command line 99% of the time because, uh, habit.
| Command | Notes |
|---|---|
| git clone git@gitlab.com:windofkeltia/project-name.git | clone a (GitLab/GitHub/Bitbucket/etc.) repository locally |
| git checkout -b branchname | create new branch and switch to its local repository |
| git fetch, git pull | latest from branch's remote repository |
| $ gvim (or IntelliJ IDEA) filename | edit, make changes to filename |
| git status | observe current repository's status, list of changed files |
| git diff filename | examine diff of changes to filename before committing |
| git add filename [ filenames ] | file or files to list of files published, ready to commit to local repository |
| git commit -m "message" | commit all published files to local repository |
| git commit -m "message" filepath | commit only filepath to local repository |
| git commit --amend -m "message" | amend the last commit (if not pushed) |
| git log --name-only | status of committed files, only filepaths |
| git log --name-status | status of committed files including filepaths |
| git log --stat | status of committed files including filepaths and commit information |
| git push | push to remote repository, good back-up strategy |
| git branch | list branches |
| git diff branchname | differences between branchname and current branch |
| git fetch | download changes from remote into local repository |
| git pull | download changes from remote into local repository and merge |
| git merge branchname | merge branchname into current branch |
| git merge branchname | merge branchname into current branch |
| git branch --merged | lists branches merged into HEAD of tip |
| git branch --merged main | lists branches merged into main |
| git branch --merged main | lists branches not yet merged |
| git branch --delete branchname | remove unneeded repository; won't delete if unpushed changes |
| git branch --delete --force branchname | when git pushes back, but you're certain or don't care |
| git checkout -b remotename origin/remotename branch 'remotename' set up to track 'origin/remotename' |
switch to a remote branch for which you have no local (after newly installed workstation?) |
| Command | Notes |
|---|---|
|
gvim (or IntelliJ IDEA) path-to-file-with-conflict git add path-to-corrected-file |
to resolve merge conflict... |
...with files of my private branch in disarray, I want to update master, then come back to my private branch. How to switch to master, perform a git pull, then return to private branch? Follow these steps:
$ git stash save "Private branch work"
$ git checkout master
$ git pull origin master
$ git checkout private branchname
$ git stash pop
Merge master's changes into my branch and favor master's changes discarding whatever (in mine) was different.
$ git merge master Auto-merging project/src/main/resources/config/file.json CONFLICT (content): Merge conflict in project/src/master/resources/config/file.json Automatic merge failed; fix conflicts and then commit the result. $ gs On branch my-enhancements Your branch is up to date with 'origin/my-enhancements'. You have unmerged paths. (fix conflicts and run "git commit") (use "git merge --abort" to abort the merge) Changes to be committed: ... Unmerged paths: (use "git add..." to mark resolution) both modified: project/src/main/resources/config/file.json $ git checkout --theirs . Updated 1 path from the index $ git add . $ git commit -m "Merged branch master into this branch; resolved conflicts by favoring master's changes." [my-enhancements e0a8073] Merged branch master into this branch; resolved conflicts by favoring main's changes.