| title | Git survival cheat sheet |
|---|---|
| datePublished | Sun May 21 2023 21:32:12 GMT+0000 (Coordinated Universal Time) |
| cuid | clhxxp7e800000al788s98iwz |
| slug | git-survival-cheat-sheet |
| cover | https://cdn.hashnode.com/res/hashnode/image/stock/unsplash/eI-nOb1K5gE/upload/0da5eb21a691b6e0ea6ce634aceacb23.jpeg |
| tags | github, git, bitbucket, gitcommands |
git config --global user.name "Nikolay Neupokoev"
git config --global user.email "mikolasan@twitter.com"git config --global core.editor vim # or nano
git config --global init.defaultBranch masterFind a gitignore file for your stack on https://gitignore.io
git init
git add .
git commit -m "Initial commit"After you created a remote repo on GitHub, GitLab, Bitbucket etc
git remote add origin <url to git repo>
git push -u origin master
git clone <url>Some projects like boost, Qt, and user dotfiles use submodules
cd <project name>
git submodule update --initCreate a separate branch
git checkout -b <branch name>Branch name conventions
-
git flow (feature/new-button fix/wrong-color release/1.0.2)
-
GitHub flow (some-meaningful-name)
Commit your work
git add -p
git commitHave more changes to previous commit? add and
git commit --amendWant to undo the last commit?
git reset HEAD^In any unexplainable situation do not panic and use
git statusit always explains where you are and gives you options of what you can do.
Now you are ready to publish your changes and make a Pull Request
git push origin branch-name-with-your-workIf you use Pull Requests on GitHub or BitBuclet then branches will be merged automatically when the Pull Request is accepted. But if you are just messing with branches then merge your branch into master
git checkout master
git merge branch-name-with-your-work
and delete your branch
git branch -d branch-name-with-your-work
And then push only the master branch.
git push origin master
It makes sense to push your branch to the remote if someone else in your team needs these changes before they are approved, or for backup purposes if the task takes longer than a day and you think that your work can blow up your computer.
In any case, do not forget to clean up after yourself on the remote. When the branch was merged to master, delete that thing that was pushed before.
git push --delete origin branch-name-with-your-work
When someone else does this in your team, this is when you need
git fetch -p origin
But more context about this later.
Get new updates from a remote repo. In simple cases, one pull is enough
git pullbut sometimes you need
git checkout master
git fetch -p origin # download remote changes only (+ update info about removed branches on the remote). if there is nothing new then stop here, no need to pull
git stash # in case there are local staged/unstaged changes, we hide them
git pull
git stash pop # return hidden changesIn case you already did some local commits, and there are remote changes, and you prefer straight-forward git history then rebase your changes on top of remote commits
git pull --rebase
The situation is simpler if you work in your branch. Then you pull easily and then rebase. To move your changes on top of the latest repo state
git rebase master # when current branch is branch-name-with-your-work
git rebase master branch-name-with-your-work # when current branch is something else, master, for exampleRestore a file once deleted
git rev-list -n 1 HEAD -- <file>
git checkout <hash from the previous command>^ -- <file>