Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure everything has been pushed with git?

I like to remove repos that I'm not working on from my computer. I check something out, I work on it and when I'm finished I push everything and delete the folder from my computer. That way things stay tidy, it's easy to get an overview of what I'm actually working on and I know that I don't have any local stuff waiting to be pushed.

But...

Before deleting my local repo I want to make sure everything has been pushed to my remote. The process I go through is usually these three steps:

git st    # check if there's something I haven't committed

git stash list    # check if I've stashed something

git log --oneline --decorate --all    # check if all branches have been pushed

I would like to simplify this. Especially the last step, which requires me to look at all my branches and see if the local and the remote ones are in sync. To be really sure, I might even have to scroll down a bit to make sure I'm not missing anything.

I'm considering writing a script for doing all of this automatically, but maybe there's a solution out there already? (I take it I don't have to emphasise that I want this done on the command line and not using any fancy GUIs :D)

How do you guys approach this? What's your process for checking that you're not forgetting anything? What tools do you use? All ideas are welcome!

like image 789
Jakob Avatar asked Feb 14 '13 10:02

Jakob


People also ask

How do you check what will be pushed in git?

In Git, we can use git show commit_id --name-only to list all the committed files that are going to push to the remote repository.

Does git push push everything?

By default, git push only updates the corresponding branch on the remote. So, if you are checked out to the main branch when you execute git push , then only the main branch will be updated. It's always a good idea to use git status to see what branch you are on before pushing to the remote.

Does git push push to all branches?

No, git push only pushes commits from current local branch to remote branch that you specified in command.


2 Answers

To know if a local and a remote branch are in sync you need to compare the hashes of the HEAD revisions.

When you execute git branch -v -a you will receive a list of local and remote branches along with the hashes of the corresponding HEAD revisions:

$ git branch -v -a
  develop                             44e61b5 <Commit message>
  feature/CodeContracts               c26edee <Commit message>
* feature/Foo                         3a40e22 <Commit message>
  master                              e68e28a <Commit message>
  remotes/origin/HEAD                 -> origin/master
  remotes/origin/develop              44e61b5 <Commit message>
  remotes/origin/feature/Bar          be9666c <Commit message>
  remotes/origin/master               e68e28a <Commit message>

As you can easily see, develop and master are up-to-date locally and remote, feature/Bar doesn't exist locally and feature/CodeContracts and feature/Foo don't exist remotely, so they should be pushed before deleting the local repository.

like image 151
Daniel Hilgarth Avatar answered Oct 01 '22 07:10

Daniel Hilgarth


Building on Daniel Hilgarth's answer, I'd recommend the following steps

First, run git fetch --all to make sure you have the latest changes from all upstream repositories

Then run git branch -vva - (note the double -v). This will print all branches (a), and for each branch the latest commit, and information on the upstream branch (if an upstream is set).

The result will look like this:

  br1                   88006cd Branch
  br2                   0b68b6f [origin/br2] New commit
  c                     9ed6569 Dummy
* master                f4664d4 [origin/master: ahead 1] my commit
  remotes/origin/HEAD   -> origin/master
  remotes/origin/br3    9ed6569 Dummy
  remotes/origin/master 9ed6569 Dummy

You can see you have four local branches (master, c, br1, br2). master and br2 have upstream branches. br2 is fully pushed, master has one local commit that is not yet pushed ("ahead 1").

br1 and c are local branches without upstream information. For these you must check and decide what to do - they may be fully merged, and redundant; or you may want to push them upstream; or you may want to merge them locally, e.g. to master, and then push that. To see branches that are not fully merged into your local master, use git branch --no-merged master. In this example, this would print "br1 br2" (because branch c is an ancestor of master).

Finally, you can see that for br2 the upstream branch origin/br2 is listed, which does not exist. This means that the remote repository has deleted this branch (and you fetched this deletion using "git remote prune" or "git fetch -p"). Again, you must decide whether you want to discard your local commits, or re-push or merge them.

like image 41
sleske Avatar answered Oct 01 '22 06:10

sleske