How to deal with Git A-Z?
文章目录
- How to modify file and update to remote repository?
- How to change last commit?
- How to delete files in remote repository?
- Pull or Fetch?
- So, what if files conflict after git pull (auto merge failed)
- How to overwrite, rather than merge, one branch from another
- How to delete a branch?
- How to roll back to a specific commit?
- What is commit -a?
- How to git ignore something except a certain file?
- How to give up all the changes since last commit?
- How to clone a specific branch
In this post I am going to show you some common scenarios you may suffer when you use git. To benefit from this tutorial please make sure you have already set-up a git environment. If not, I would advise you to take a look at here.
How to modify file and update to remote repository?
git add file
git commit -m "Aha, file modified"
Or, just type
git commit -am "Aha, file modified"
After that, push to remote repository:
git push origin branch_name
So, what’s difference between these two? I will reach to that later.
How to change last commit?
It’s a good practice to fix a wrong commit rather than make a new commit.
So, first, edit the file with the problem, make the corrections, then:
git add now_right_file
git commit --amend
git push --force branch_name # Warning!
Be careful, The --force
is dangerous, it works fine to me for 99% cases, but it dose have potential harmness, and that’s why Linus doesn’t recommend it.
How to delete files in remote repository?
There are two ways of delete files, delete locally and commit to remote repository, or just directly delete files in remote repository, like:
git rm --cached file_to_delete
Even better, you can delete all the files match a certain glob:
git rm --cached 'merge-*' # delete all the files start with "merge-"
Pull or Fetch?
There already has an excellent and well accepted answer on StackOverflow, it’s way much better than my explanation –> link:
In the simplest terms,
git pull
does agit fetch
followed by agit merge
.
You can do agit fetch
at any time to update your remote-tracking branches underrefs/remotes/<remote>/
. This operation never changes any of your own local branches underrefs/heads
, and is safe to do without changing your working copy. I have even heard of people running git fetch periodically in a cron job in the background (although I wouldn’t recommend doing this).A
git pull
is what you would do to bring a local branch up-to-date with its remote version, while also updating your other remote-tracking branches.
So, what if files conflict after git pull (auto merge failed)
git pull # will auto merge unconflicted part
git status # check the information of conflicted files
Use your favorite editor to edit the conflicted file with “<<<<<<” and “>>>>>>”, save it, commit it, that’s all.
How to overwrite, rather than merge, one branch from another
git checkout latest_branch
git merge -s ours to_overwrite_branch
What’s the ours
means here? It’s a merge strategy, you can find it in git checkout doc:
git checkout [--ours| theirs] branch --ours --theirs
When checking out paths from the index, check out stage #2 (ours) or #3 (theirs) for unmerged paths.
The index may contain unmerged entries because of a previous failed merge. By default, if you try to check out such an entry from the index, the checkout operation will fail and nothing will be checked out. Using -f will ignore these unmerged entries. The contents from a specific side of the merge can be checked out of the index by using –ours or –theirs. With -m, changes made to the working tree file can be discarded to re-create the original conflicted merge result.
How to delete a branch?
git branch -d died_branch
git push origin --delete die_branch # or git push origin :died_branch
How to roll back to a specific commit?
git reflog show # find revision hash
git checkout revision_hash .
I will explain this a little bit. git reflog show
gives us a list of all the commits and their hashes. Then, checkout that specific hash.
Read more:
What is commit -a?
Many people always ask how to combine git add
and git commit
in one command, and the most answered solution is git commit -a -m "blah blah"
.
Yes and no. For the files which have been git add
before, git commit -a
will do the git add
for you. But for rest files(aka untracked files), we have to the git add
. If you really want to save the time for these tedious work, alias is what you are looking for.
How to git ignore something except a certain file?
In .gitignore:
# Ignore everything
*
!except_script.sh
This will ignore everything but except_scrpit.sh.
How to give up all the changes since last commit?
Once my silly cat was dancing on my keyboard after a commit mess up all the files!
Luckily, we can use
git reset hard --HEAD^
to revert to the previous commit.
Or, I wrongly git add should_not_add_file
, we can also use
git reset HEAD should_not_add_file
to upstage that file.
How to clone a specific branch
Stolen from Stackoverflow again
git clone -b <branch> <remote_repo>
Example:
git clone -b my-branch [email protected]:user/myproject.git
Alternative (no public key setup needed):
git clone -b my-branch https://[email protected]/username/myproject.git